package org.demo.db.meta.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.demo.db.meta.Constraint;
import org.demo.db.meta.Field;
import org.demo.db.meta.Index;
import org.demo.db.meta.Table;

/**
 * Oracle -> 元数据信息
 * @author  
 * @date    2010-9-6
 * @file    org.demo.db.meta.impl.OracleMetaImpl.java
 */
public class OracleMetaImpl extends CommonDbMetaImpl {
	/**
	 * 构造函数
	 * @param driver
	 * @param url
	 * @param user
	 * @param password
	 * @throws Exception
	 */
	public OracleMetaImpl(String driver,String url,
		String user,String password)throws Exception{
		super(driver,url,user,password);
	}
	/**
	 * 构造函数
	 * @param conn
	 */
	public OracleMetaImpl(Connection conn)throws SQLException{
		super(conn);
	}
	
	/**
	 * 获取表信息[使用 getTables(...) 会获取到 oracle 回收站中的表，进而引起后续操作中的错误.]
	 * @return
	 * @throws SQLException
	 */
	@Override
	public List<Table> getTables() throws SQLException {
		String schema = getCurrentSchema();
		List<Table> tables = new ArrayList<Table>();
		
		Statement stmt = super.conn.createStatement();
		ResultSet rs = stmt.executeQuery("SELECT TABLE_NAME FROM USER_TABLES");
		while(rs.next()){
			String tableName = rs.getString("TABLE_NAME");
			List<Field> fields = getFields(schema, tableName);
			List<Constraint> pks = getPrimaryKeys(schema, tableName);
			List<Constraint> fks = getForeignKeys(schema, tableName);
			List<Index> indexes = getIndexes(schema, tableName);
			
			Table table = new Table();
			table.setSchema(schema);
			table.setName(tableName);
			table.setRemarks("");
			table.setTableType("TABLE");
			table.setFields(fields);
			table.setPrimaryKeys(pks);
			table.setForeignKeys(fks);
			table.setIndexes(indexes);
			tables.add(table);
		}
		// 关闭游标
		rs.close();
		return tables;
	}
}
